layui.use(['table', 'admin', 'ax', 'func','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;

    /**
     * 帖子类型表管理
     */
    var Category = {
        tableId: "categoryTable"
    };

    /**
     * 初始化表格的列
     */
    Category.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '类型编码'},
            {field: 'name', align: "center",sort: true, title: '类型名字'},
            {field: 'typeName', align: "center",sort: true, title: '类型分组'},
            {field: 'status',  align: "center",sort: true, title: '状态', templet: '#statusTpl'},
            {field: 'sort',align: "center", sort: true, title: '排序'},
            {field: 'createTime',align: "center", sort: true, title: '添加时间'},
            {field: 'updateTime', align: "center",sort: true, title: '修改时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Category.search = function () {
        var queryData = {};
        queryData['keyword'] = $("#condition").val();
        table.reload(Category.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    Category.openAddDlg = function () {
        func.openFull({
            title: '添加帖子类型',
            content: Feng.ctxPath + '/category/add',
            tableId: Category.tableId,
            maxmin: true,
            shade: .2,
            full:false
            //skin:'layui-layer-admin',
        });

        // func.open({
        //     title: '添加帖子类型表',
        //     content: Feng.ctxPath + '/category/add',
        //     tableId: Category.tableId
        // });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    Category.openEditDlg = function (data) {
        func.openFull({
            title: '修改帖子类型',
            content: Feng.ctxPath + '/category/edit?id=' + data.id,
            tableId: Category.tableId,
            maxmin: false,
        });

        // func.open({
        //     title: '修改帖子类型表',
        //     content: Feng.ctxPath + '/category/edit?id=' + data.id,
        //     tableId: Category.tableId
        // });
    };

    /**
     * 导出excel按钮
     */
    Category.exportExcel = function () {
        var checkRows = table.checkStatus(Category.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Category.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/category/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Category.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Category.tableId,
        url: Feng.ctxPath + '/category/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Category.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Category.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Category.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Category.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Category.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Category.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Category.onDeleteItem(data);
        }
    });

    form.on('switch(status)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? "ENABLE" : "DISABLE";
        Category.changeStatus(id, checked);
    });

    /**
     * 修改启用状态
     */
    Category.changeStatus = function (id, checked) {
        var ajax = new $ax(Feng.ctxPath + "/category/editItem", function (data) {
            Feng.success("修改成功!");
        }, function (data) {
            Feng.error("修改失败!" + data.responseJSON.message);
            table.reload(Category.tableId);
        });
        ajax.set("id", id);
        ajax.set("status", checked);
        ajax.start();
    };
});
